package weaver.aiyh_pcn.workflow_to_customer.utils;

import com.weaver.general.Util;
import org.h2.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/8/12 0012 13:20
 * 构建SQL工具类
 */


public class BuilderSqlUtil {
	public static String builderInsertFullSql(String tableName, Map<String, Object> fieldsValuesMap){
		if(Objects.isNull(fieldsValuesMap) || StringUtils.isNullOrEmpty(tableName)){
			return null;
		}
		fieldsValuesMap = Utils.createMap().uPutAll(fieldsValuesMap)
				.filter((key, value) -> !Objects.isNull(value));
		if(fieldsValuesMap.size() == 0){
			return null;
		}
		StringBuilder sqlBuilder = new StringBuilder("insert into ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		StringBuilder sqlValueBuilder = new StringBuilder();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" ( ");
		fieldsValuesMap.forEach((field,value) -> {
			sqlFieldBuilder.append(field);
			sqlFieldBuilder.append(", ");
			sqlValueBuilder.append("'");
			sqlValueBuilder.append(value);
			sqlFieldBuilder.append("'");
			sqlValueBuilder.append(", ");
		});
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" ) values ( ");
		sqlBuilder.append(Utils.removeSeparator(sqlValueBuilder));
		sqlBuilder.append(" )");
		return sqlBuilder.toString();
	}

	public static SqlResult builderInsertSql(String tableName, Map<String, Object> fieldsValuesMap){
		if(Objects.isNull(fieldsValuesMap) || StringUtils.isNullOrEmpty(tableName)){
			return null;
		}
		StringBuilder sqlBuilder = new StringBuilder("insert into ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		StringBuilder sqlValueBuilder = new StringBuilder();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" ( ");
		Object[] args = new Object[fieldsValuesMap.size()];
		AtomicInteger i = new AtomicInteger();
		fieldsValuesMap.forEach((field,value)->{
			sqlFieldBuilder.append(field);
			sqlFieldBuilder.append(", ");
			sqlValueBuilder.append("?, ");
			args[i.getAndIncrement()] = Util.null2String(String.valueOf(value));
		});
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" ) values ( ");
		sqlBuilder.append(Utils.removeSeparator(sqlValueBuilder));
		sqlBuilder.append(" )");
		return new SqlResult(sqlBuilder.toString(),args);
	}

	public static SqlResult builderBatchInsertSql(String tableName, List<Map<String, Object>> fieldsValuesMapList){
		if(Objects.isNull(fieldsValuesMapList) || StringUtils.isNullOrEmpty(tableName)){
			return null;
		}
		StringBuilder sqlBuilder = new StringBuilder("insert into ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		StringBuilder sqlValueBuilder = new StringBuilder();
		List<List> args = new ArrayList<>();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" ( ");
		AtomicInteger i = new AtomicInteger();
		if(fieldsValuesMapList.size() >= 1){
			fieldsValuesMapList.forEach(item->{
				if(i.get() == 0){
					item.forEach((field,value)->{
						sqlFieldBuilder.append(field);
						sqlFieldBuilder.append(", ");
						sqlValueBuilder.append("?, ");
					});
					i.getAndIncrement();
				}
				ArrayList<Object> list = new ArrayList<>();
				item.forEach((field,value)-> list.add(value));
				args.add(list);
			});
		}
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" ) values ( ");
		sqlBuilder.append(Utils.removeSeparator(sqlValueBuilder));
		sqlBuilder.append(" )");
		return new SqlResult(sqlBuilder.toString(),args);
	}

	public static String builderUpdateFullSql(String tableName, Map<String, Object> fieldsValuesMap, String conditions){
		if(Objects.isNull(fieldsValuesMap) || StringUtils.isNullOrEmpty(tableName)){
			return null;
		}
		fieldsValuesMap = Utils.createMap().uPutAll(fieldsValuesMap)
				.filter((key, value) -> !Objects.isNull(value));
		if(fieldsValuesMap.size() == 0){
			return null;
		}
		StringBuilder sqlBuilder = new StringBuilder("update ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" set ");
		fieldsValuesMap.forEach((field, value)->{
			sqlFieldBuilder.append(field);
			sqlFieldBuilder.append(" = ");
			sqlFieldBuilder.append("'");
			sqlFieldBuilder.append(value);
			sqlFieldBuilder.append("'");
			sqlFieldBuilder.append(", ");
		});
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" where ");
		sqlBuilder.append(conditions);
		return sqlBuilder.toString();
	}

	public static String builderUpdateFullSql(String tableName, Map<String, Object> fieldsValuesMap, SQLWhere sqlWhere){
		if(Objects.isNull(fieldsValuesMap) || StringUtils.isNullOrEmpty(tableName)){
			return null;
		}
		fieldsValuesMap = Utils.createMap().uPutAll(fieldsValuesMap)
				.filter((key, value) -> !Objects.isNull(value));
		if(fieldsValuesMap.size() == 0){
			return null;
		}
		StringBuilder sqlBuilder = new StringBuilder("update ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" set ");
		fieldsValuesMap.forEach((field,value) -> {
			sqlFieldBuilder.append(field);
			sqlFieldBuilder.append(" = ");
			sqlFieldBuilder.append("'");
			sqlFieldBuilder.append(value);
			sqlFieldBuilder.append("'");
			sqlFieldBuilder.append(", ");
		});
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" where ");
		sqlBuilder.append(sqlWhere.getSqlWhere());
		return sqlBuilder.toString();
	}
	public static SqlResult builderUpdateSql(String tableName, Map<String, Object> fieldsValuesMap, PrepSQLWhere prepSQLWhere){
		StringBuilder sqlBuilder = new StringBuilder("update ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		List<Object> args = new ArrayList<>();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" set ");
		fieldsValuesMap.forEach((field, value)->{
			sqlFieldBuilder.append(field);
			sqlFieldBuilder.append(" = ? ");
			sqlFieldBuilder.append(", ");
			args.add(value);
		});
		args.addAll(prepSQLWhere.getArgs());
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" where ");
		sqlBuilder.append(prepSQLWhere.getPstSqlWhere());
		return new SqlResult(sqlBuilder.toString(),args.toArray(new Object[args.size()]));
	}

	public static SqlResult builderBatchUpdateSql(String tableName, List<Map<String, Object>> fieldsValuesMapList, PrepSQLWhere prepSQLWhere){
		StringBuilder sqlBuilder = new StringBuilder("update ");
		StringBuilder sqlFieldBuilder = new StringBuilder();
		sqlBuilder.append(tableName);
		sqlBuilder.append(" set ");
		List<List> args = new ArrayList<>();
		fieldsValuesMapList.forEach(item->{
			item.forEach((field, value)->{
				sqlFieldBuilder.append(field);
				sqlFieldBuilder.append(" = ? ");
				sqlFieldBuilder.append(", ");
			});
			ArrayList<Object> list = new ArrayList<>();
			item.forEach((field,value)-> list.add(value));
			list.addAll(prepSQLWhere.getArgs());
			args.add(list);
		});
		sqlBuilder.append(Utils.removeSeparator(sqlFieldBuilder));
		sqlBuilder.append(" where ");
		sqlBuilder.append(prepSQLWhere.getPstSqlWhere());

		return new SqlResult(sqlBuilder.toString(),args);
	}



}
